package com.plsjavaweb.ui.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.plsjavaweb.core.ReportsCore;
import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.dao.mapper.ProjectSeatAllocationMapper;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.CSReportsPOJO;
import com.plsjavaweb.pojo.ProjectPOJO;
import com.plsjavaweb.ui.controler.modelview.ErrorPage;

import java.io.File;
import java.util.Date;

import jxl.*;
import jxl.write.*;

/**
 * Servlet implementation class PmReports
 */
@WebServlet("/PmReports")
public class PmReports extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log = Logger.getLogger(DAOFactory.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PmReports() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info(LoggingConstants.entryMessage);
		HttpSession session = request.getSession(true);
		if (session.getAttribute("user") == null) {
			log.info(LoggingConstants.sessionExpired);
			ErrorPage errorPage = new ErrorPage();
			errorPage.setErrorTitle("Error Page");
			errorPage.setErrorHeader("Invalid Session");
			errorPage
					.setErrorContent("Session has expired. Please Log-in to start new session");
			errorPage.setReturnUrl("/pls-java-web/Login");
			errorPage.setReturnUrlTitle("Go Back to Login Page");
			request.setAttribute("error", errorPage);
			RequestDispatcher rd = request
					.getRequestDispatcher("error-page.jsp");
			rd.forward(request, response);
		} else {
			String role = (String) session.getAttribute("role");
			if (!role.equalsIgnoreCase("MANAGER")) {
				response.sendRedirect("Home");
				return;
			}
			int managerId = (Integer) session.getAttribute("employeeId");
			ReportsCore pmReportsCore = new ReportsCore();
			List<ProjectPOJO> projects = new ArrayList<ProjectPOJO>();

			projects = pmReportsCore.getProjectSeatByManager(managerId);
			request.setAttribute("projects", projects);
			session.setAttribute("projects", projects);

			request.getRequestDispatcher("WEB-INF/reports_pm.jsp").forward(
					request, response);
		}
	}

}
